var map = [];
var wl; //边长
var boom;
var redBox = [];
var greenBox = [];
var right = 0;
var tag = 0;
function Start(a,b){
    CreatMap(a,b);
    CreatRedBox();
    CreatBoom(b);
    //show();
    writeIn();
    addListener();
}

//创建地图数据
function CreatMap(a,b){
    wl = a;
    boom = b;
    for(var i=0;i<a;i++){
        map[i] = new Array();
        for(var j=0;j<a;j++){
            map[i][j] = 0;
        }
    }
}
function CreatBoom(b){
    var x,y;
    var j = 0;
    for(var i=0;i<b;i++){
        while(true){
            x = Math.floor(Math.random()*wl);
            y = Math.floor(Math.random()*wl);
            if(isOK(x,y)){
                map[x][y] = 9;
                redBox[0][j] = parseInt(x)+1;
                redBox[1][j] = parseInt(y)+1;
                j++;
                roundIt(x,y);
                break;
            }
        }
    }
}
function isOK(x,y){
    if(map[x][y]==9){
        return false;
    }
    return true;
}
function roundIt(x,y){
    for (var i=-1;i<2;i++) {
        plus(x-1,y+i)
        plus(x+1,y+i)
    }
    plus(x,y+1)
    plus(x,y-1)
}
function plus(x,y){
    if (x>=0 && x<wl && y>=0 && y<wl) {
        if(map[x][y]!=9) {
            map[x][y]+=1;
        }
    }
}
function CreatRedBox(){
    for(var i=0;i<2;i++){
        for(var j=0;j<boom;j++){
            redBox[i] = new Array();
            redBox[i][j] = 0;
        }
    }
}
function show(){
    for(var i=0;i<wl;i++){
        for(var j=0;j<wl;j++){
            document.write(map[i][j]);
        }
        document.write("<br>");
    }
    for(var i=0;i<2;i++){
        for(var j=0;j<boom;j++){
            document.write(redBox[i][j]+1+" ");
        }
        document.write("<br>");
    }
}

//创建地图
function writeIn(){
    var x = document.querySelector(".gameBox");
    for (var i = 0; i < wl; i++) {
        x.innerHTML = x.innerHTML + "<ul class='row' id='"+(i+1)*10+"'></ul>";
    }

    var y = document.querySelectorAll(".row");
    for(var i=0;i<wl;i++){
        for(var j=0;j<wl;j++){
            var number = map[i][j];
            y[i].innerHTML += "<li class='col' id='"+(j+1)+"'><span>"+number+"<span></li>";
        }
    }
}
function addListener(){
    var x = document.querySelectorAll(".row");
    for(var i=0;i<x.length;i++){
        x[i].addEventListener("click",function(event){
            var el = event.target;
            if (el.tagName != 'LI') {
                el = event.target.parentElement;
            }
            if(el.children[0].innerText != "9" && el.style.background != "white"){
                if(el.children[0].innerText == "0"){
                    showRound(el);
                }
                el.children[0].style.opacity = "1";
                el.style.background = "white";
            }else if(el.children[0].innerText == "9"){
                el.style.background = "red";
                window.alert("菜");
            }
        })
    }
    for(var i=0;i<2;i++){
        for(var j=0;j<boom;j++){
            greenBox[i] = new Array();
            greenBox[i][j] = 0;
        }
    }
    for(var j=0;j<x.length;j++){
        x[j].addEventListener("contextmenu",function(event){
            event.preventDefault();
            var el = event.target;
            if (el.tagName != 'LI') {
                el = event.target.parentElement;
            }
            if(el.style.background != "white" && el.tagName == "LI" && tag<boom){
                var x = el.parentElement.id/10;
                var y = el.id;
                if(checkGreen(x,y)){
                    deleteGreen(x,y);
                    el.style.background = "darkgray";
                }else{
                    addGreen(x,y);
                    el.style.background = "green";
                }
            }
            if(tag==boom){
                isWin();
            }
        })
    }
}
function showRound(el){
    var dadel = el.parentElement;
    var x = dadel.id;
    var y = el.id;
    CshowRound(x,y);
}
function CshowRound(x,y){
    //console.log(x+" "+y);
    if(x/10 > wl || y > wl || x <= 0 || y <= 0){
        return;
    }else{
        var el = document.getElementById(x).children[y-1];
        if(el.children[0].innerText == 0 && el.style.background !== "white"){
            el.children[0].style.opacity = "1";
            el.style.background = "white";
            CshowRound((x-10), (y-1)); //左上
            CshowRound((x-10), y); //上
            CshowRound((parseInt(x)+10), (parseInt(y)+1)); //右下
            CshowRound((parseInt(x)+10), (y-1)); //左下
            CshowRound((x-10), (parseInt(y)+1)); //右上
            CshowRound((parseInt(x)+10), y); //下
            CshowRound(x, (parseInt(y)+1)); //右
            CshowRound(x, (y-1)); //左
        }
    }
}
function checkGreen(x,y){
    for(var i=0;i<2;i++){
        if(x == greenBox[0][i]){
            for (var j=0;j<boom;j++){
                if(y == greenBox[1][j]){
                    return true;
                }
            }
        }
    }
    return false;
}
function deleteGreen(x,y){
    for(var j=0;j<boom;j++){
        if(x==greenBox[0][j] && y==greenBox[1][j]){
            for(var i=j;i<boom-1;i++){
                greenBox[0][i] = greenBox[0][i+1];
                greenBox[1][i] = greenBox[1][i+1];
            }
            greenBox[0][i] = 0;
            greenBox[1][i] = 0;
        }
    }
    tag--;
    for(var j=0;j<boom;j++){
        console.log("delete "+greenBox[0][j]+" "+greenBox[1][j]);
    }
}
function addGreen(x,y){
    greenBox[0][tag] = x;
    greenBox[1][tag] = y;
    tag ++;
    for(var j=0;j<boom;j++){
        console.log("add "+greenBox[0][j]+" "+greenBox[1][j]);
    }
}
function isWin(){
    var flag = 0;
    for(var i=0;i<2;i++){
        for(var j=0;j<boom;j++){
            console.log("red:"+i+" "+j+" "+redBox[i][j]);
        }
    }
    for(var i=0;i<2;i++){
        for(var j=0;j<boom;j++){
            console.log("gre:"+i+" "+j+" "+greenBox[i][j]);
        }
    }
    for(var i=0;i<boom;i++){
        for(var j=0;j<boom;j++){
            if(greenBox[0][i]==redBox[0][j]){
                if(greenBox[1][i]==redBox[1][j]){
                    flag++;
                    console.log(flag);
                }
            }
        }
    }
    if(flag==boom){
        window.alert("win");
    }else{
        window.alert("lose");
    }
}









//一个很可爱的方法 舍不得删

/*function CshowRound1(x,y){
    console.log(x/10+" "+y);
    if(x/10 > wl || y > wl || x < 0 || y < 0){
        return;
    }
    var el = document.getElementById(x).children[y-1];
    if(el.children[0].innerText == 0 && el.style.background !== "white"){
        el.children[0].style.opacity = "1";
        el.style.background = "white";
        if(x/10==1){
            if(y==1){
                CshowRound(x + 10, y + 1); //右下
                CshowRound(x + 10, y); //下
                CshowRound(x, y + 1); //右
            }else if(y==wl){
                CshowRound(x + 10, y - 1); //左下
                CshowRound(x + 10, y); //下
                CshowRound(x, y - 1); //左
            }
            else{
                CshowRound(x + 10, y + 1); //右下
                CshowRound(x + 10, y - 1); //左下
                CshowRound(x + 10, y); //下
                CshowRound(x, y + 1); //右
                CshowRound(x, y - 1); //左
            }
        }else if(x/10==wl){
            if(y==1){
                CshowRound(x - 10, y); //上
                CshowRound(x - 10, y + 1); //右上
                CshowRound(x, y + 1); //右
            }else if(y==wl){
                CshowRound(x - 10, y - 1); //左上
                CshowRound(x - 10, y); //上
                CshowRound(x, y - 1); //左
            }
            else{
                CshowRound(x - 10, y - 1); //左上
                CshowRound(x - 10, y); //上
                CshowRound(x - 10, y + 1); //右上
                CshowRound(x, y + 1); //右
                CshowRound(x, y - 1); //左
            }
        }else{
            if(y==1){
                CshowRound(x - 10, y); //上
                CshowRound(x + 10, y + 1); //右下
                CshowRound(x - 10, y + 1); //右上
                CshowRound(x + 10, y); //下
                CshowRound(x, y + 1); //右
            }else if(y==wl){
                CshowRound(x - 10, y - 1); //左上
                CshowRound(x - 10, y); //上
                CshowRound(x + 10, y - 1); //左下
                CshowRound(x + 10, y); //下
                CshowRound(x, y - 1); //左
            }else{
                CshowRound(x - 10, y - 1); //左上
                CshowRound(x - 10, y); //上
                CshowRound(x + 10, y + 1); //右下
                CshowRound(x + 10, y - 1); //左下
                CshowRound(x - 10, y + 1); //右上
                CshowRound(x + 10, y); //下
                CshowRound(x, y + 1); //右
                CshowRound(x, y - 1); //左
            }
        }
    }
}*/